﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.Win32;
using System.Collections.Specialized;

namespace uLib.Helper
{
    public class SystemHelper
    {
        public DateTime WindowsStartTime()
        {
            DateTime dt = new DateTime();
            dt = DateTime.Now - new TimeSpan(0, 0, 0, 0, System.Environment.TickCount);
            return dt;
        }

        public string getDisplayResolution()
        {
            int Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            int Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

            string res = Width.ToString() +"x" + Height.ToString();
            return res;
        }

        public uint CPUSpeed()
        {
            ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'");
            uint sp = (uint)(Mo["CurrentClockSpeed"]);
            Mo.Dispose();
            return sp;
        }

        public static string getWorkgroupName()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_ComputerSystem");
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    return queryObj["Workgroup"].ToString();
                }
            }
            catch (Exception e)
            {
                throw new ManagementException("Error retrieving workgroup name.", e);
            }
            throw new ManagementException("Error retrieving workgroup name.");
        }

        public static string getDomainName()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_ComputerSystem");
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    return queryObj["Domain"].ToString();
                }
            }
            catch (Exception e)
            {
                throw new ManagementException("Error retrieving domain name.", e);
            }
            throw new ManagementException("Error retrieving domain name.");
        }

        public void SetStartup(string AppName, bool enable)
        {
            string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

            Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);

            if (enable)
            {
                if (startupKey.GetValue(AppName) == null)
                {
                    startupKey.Close();
                    startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                    startupKey.SetValue(AppName, Application.ExecutablePath.ToString());
                    startupKey.Close();
                }
            }
            else
            {
                startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                startupKey.DeleteValue(AppName, false);
                startupKey.Close();
            }
        }

        public static StringCollection installedPrograms()
        {
            StringCollection allPrograms = new StringCollection();
            string regKeyPfad = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";
            RegistryKey regKey = Registry.LocalMachine.OpenSubKey(regKeyPfad);

            if (regKey != null)
            {
                string[] subKeyNames = regKey.GetSubKeyNames();

                for (int i = 0; i < subKeyNames.Length; i++)
                {
                    RegistryKey subKey = regKey.OpenSubKey(subKeyNames[i]);
                    string programName = (string)subKey.GetValue("DisplayName");

                    if (programName == null)
                    {
                        programName = (string)subKey.GetValue("QuitDisplayName");
                    }
                    if (programName != null)
                    {
                        allPrograms.Add(programName);
                    }
                }
            }
            else
            {
                throw new Exception("Registry-Schluessel"
                    + Registry.LocalMachine.Name
                    + "\\" + regKeyPfad
                    + " nicht gefunden");
            }
            return allPrograms;
        }


        public void catchInstalledPrograms(ListBox listbox)
        {
            StringCollection st = new StringCollection();

            st = installedPrograms();

            for (int i = 0; i < st.Count - 1; i++)
            {
                if (st[i].Contains("Hotfix") || st[i].Contains("Sicherheitsupdate") || st[i].Contains("Update"))
                {
                    st.RemoveAt(i);
                }
            }

            foreach (string programs in st)
            {

                listbox.Items.Add(programs);
            }
        }
    }
}
